/**
 * Copyright 2014-2017 yangming.liu<bytefox@126.com>.
 * <p>
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 * <p>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 * <p>
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, see <http://www.gnu.org/licenses/>.
 */
package org.feisoft.jta.supports.dubbo.validator;

import org.feisoft.jta.supports.dubbo.DubboConfigValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;

public class ServiceConfigValidator implements DubboConfigValidator
{
    static final Logger logger = LoggerFactory.getLogger(ServiceConfigValidator.class);

    private String beanName;
    private BeanDefinition beanDefinition;

    public void validate() throws BeansException
    {
        MutablePropertyValues mpv = this.beanDefinition.getPropertyValues();
        PropertyValue retries = mpv.getPropertyValue("retries");
        PropertyValue loadbalance = mpv.getPropertyValue("loadbalance");
        PropertyValue cluster = mpv.getPropertyValue("cluster");
        PropertyValue filter = mpv.getPropertyValue("filter");
        PropertyValue group = mpv.getPropertyValue("group");

        if (retries == null || retries.getValue() == null || "-1".equals(retries.getValue()) == false)
        {
            throw new FatalBeanException(
                    String.format("The value of attr 'retries'(beanId= %s) should be '0'.", this.beanName));
        } else if (loadbalance == null || loadbalance.getValue() == null
                || "transaction".equals(loadbalance.getValue()) == false)
        {
            throw new FatalBeanException(
                    String.format("The value of attr 'loadbalance'(beanId= %s) should be 'transaction'.", this.beanName));
        } else if (cluster == null || cluster.getValue() == null || "failfast".equals(cluster.getValue()) == false)
        {
            throw new FatalBeanException(
                    String.format("The value of attribute 'cluster' (beanId= %s) must be 'failfast'.", this.beanName));
        } else if (filter == null || filter.getValue() == null || "transaction".equals(filter.getValue()) == false)
        {
            throw new FatalBeanException(
                    String.format("The value of attr 'filter'(beanId= %s) should be 'transaction'.", this.beanName));
        } else if (group == null || group.getValue() == null //
                || ("org.feisoft.jta".equals(group.getValue())
                || String.valueOf(group.getValue()).startsWith("org.feisoft.jta-")) == false)
        {
            throw new FatalBeanException(String.format(
                    "The value of attr 'group'(beanId= %s) should be 'org.feisoft.jta' or starts with 'org.feisoft.jta-'.",
                    this.beanName));
        }
    }

    public String getBeanName()
    {
        return beanName;
    }

    public void setBeanName(String beanName)
    {
        this.beanName = beanName;
    }

    public BeanDefinition getBeanDefinition()
    {
        return beanDefinition;
    }

    public void setBeanDefinition(BeanDefinition beanDefinition)
    {
        this.beanDefinition = beanDefinition;
    }

}
